package com.demo.rabbitmq.util;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * Hello world!
 */
public class ConnectionUtil {

    public static final String HOST_ADDRESS = "localhost";

    public static  Connection getConnection() throws Exception{

        //定义连接工厂
        ConnectionFactory factory = new ConnectionFactory();

        //设置服务地址
        factory.setHost(HOST_ADDRESS);

        //端口
        factory.setPort(5672);

        //设置账号信息，用户名，密码，vhost
        factory.setVirtualHost("/");
        factory.setUsername("guest");
        factory.setPassword("guest");

        //通过工厂获取连接
        Connection connection = factory.newConnection();
        return connection;
    }
    public static void main(String[] args) throws Exception{
        Connection conn = ConnectionUtil.getConnection();
        System.out.println(conn);
        conn.close();
    }
}
